/**
 ******************************************************************************
 * @file main.c
 * @author GX (2524913364@qq.com)
 * @brief 
 * @version 1.0.0 
 * @date 2022-07-04
 * 
 * @copyright Copyright (c) 2022
 * 
 *----------------------------------------------------------------------------*
 *  Remark         : Description                                              *
 *----------------------------------------------------------------------------*
 *  Change History :                                                          *
 *  <Date>     | <Version> | <Author>       | <Description>                   *
 *----------------------------------------------------------------------------*
 *  2022-07-04 | 1.0.0     | GX             | Create file                     *
 *----------------------------------------------------------------------------*
 *                                                                            *
 ******************************************************************************
 */
/*----------------------------- start of file -------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#include "lcd.h"
#include "touch_state.h"

pthread_t g_touch_id;

/**
 * @brief 线程接收到 ctrl+c 的信号后执行该响应函数释放资源
 * 
 * @param arg 
 */
static void tc_single_free(int arg)
{
	printf("\n\n [%s]关闭线程 \n\n", __FUNCTION__);
	pthread_cancel(g_touch_id);
  pthread_join(g_touch_id, NULL);
  exit(0);
}

int main(int argc, char* argv[])
{
  if (lcd_init())
  {
    printf("lcd 初始化成功\n");
  }
  else
  {
    printf("lcd 初始化失败\n");
    return -1;
  }

  /* 初始化触摸屏开启状态机-------------------------- */
  g_touch_id = InitTouch_pthread();
  signal(SIGINT, tc_single_free);

  TouchState_t tc;

  int8_t mode = 0;
  touch_mode(TC_GETMODE, &mode);
  mode |= (TC_MODE_DTAG|TC_MODE_SLIDE);
  touch_mode(TC_SETMODE, &mode);

  while(1)
  {
    if( !get_attr_touch(&tc) )
    {
      // LCD_CLEAN(g_lcd_fb);
      // DispStringLine_EN(g_lcd_fb, 1, 0, "STATE_NOT_PRESSED");
      continue;
    }

    LCD_ClearLine(g_lcd_fb, 1);   

    switch (tc.state)
		{
			case STATE_NOT_PRESSED:	  /* 无操作 */
        LCD_CLEAN(g_lcd_fb);
        printf("\t[%s]STATE_NOT_PRESSED\n", __FUNCTION__);
        DispStringLine_EN(g_lcd_fb, 1, 0, "STATE_NOT_PRESSED");
				break;

			case STATE_PRESSED:			  /* 按下 */
        LCD_CLEAN(g_lcd_fb);
        printf("\t[%s]STATE_PRESSED\n", __FUNCTION__);
				DispStringLine_EN(g_lcd_fb, 1, 0, "STATE_PRESSED");
				break;

			case STATE_RELEASE:			  /* 释放 */
        LCD_CLEAN(g_lcd_fb);
        printf("\t[%s]STATE_RELEASE\n", __FUNCTION__);
				DispStringLine_EN(g_lcd_fb, 1, 0, "STATE_RELEASE");
				break;

			case STATE_SINGLE_CLICK:  /* 单击 */
        LCD_CLEAN(g_lcd_fb);
        printf("\t[%s]STATE_SINGLE_CLICK\n", __FUNCTION__);
				DispStringLine_EN(g_lcd_fb, 1, 0, "STATE_SINGLE_CLICK");
				break;

			case STATE_SLIDE:				/* 滑屏 */
        LCD_CLEAN(g_lcd_fb);
        printf("\t[%s]STATE_SLIDING\n", __FUNCTION__);
        DispStringLine_EN(g_lcd_fb, 1, 0, "STATE_SLIDING");
        switch (tc.direction)
        {
        case TC_LEFT:
          printf("\n                        左\n");
          break;
        case TC_RIGHT:
          printf("\n                        右\n");
          break;
        case TC_UP:
          printf("\n                        上\n");
          break;
        case TC_DOWN:
          printf("\n                        下\n");
          break;
        
        default:
          break;
        }
				break;
			
      case STATE_DTAG:				/* 拖动 */
        LCD_CLEAN(g_lcd_fb);
        printf("\t[%s]STATE_DTAGING\n", __FUNCTION__);
        DispStringLine_EN(g_lcd_fb, 1, 0, "STATE_DTAGING");
				break;

			default:
				break;
		}
  }
  

  printf("exit...\n");
  lcd_close();

	return 0;
}

  




 

/*------------------------------ end of file --------------------------------*/
